﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GNSCore.Managers;
using GNSCore.Model;
using GNSCore.Services.Contracts;
using GNSCore.Services;
using GNSCore.Model.Context;
using GNSCore.Repositories;
using GNSCore.Repositories.IOC;
using GNSCore.Managers.Statistics;  
using ThirdServices.AlertModel.Providers.Wanderground.Factory;
using ThirdServices.AlertModel.Factory.BaseInterface;
using ThirdServices.AlertModel.Factory.Entities.DTO;


namespace GNSCore.Helpers
{
    /// <summary>
    /// Helper for verifing user alerts
    /// </summary>
    public static class VerificationHelper
    {
        static IRepository<Location> _locationRepository;
        static IRepository<Locale> _localeRepository;
        static IRepository<UserSettings> _userSettingsRepository;
        static IRepository<User> _userRepository;
        static IRepository<Model.GeoData> _geoDataRepository;
        static IRepository<Model.GeoExtensionData> _geoExtensionDataRepository;
        static IGNSContext _context;

        static VerificationHelper()
        {
            _locationRepository = IOCManager.IOCContainer.Resolve<IRepository<Location>>();
            _localeRepository = IOCManager.IOCContainer.Resolve<IRepository<Locale>>();
            _userRepository = IOCManager.IOCContainer.Resolve<IRepository<User>>();
            _userSettingsRepository = IOCManager.IOCContainer.Resolve<IRepository<UserSettings>>();
            _context = IOCManager.IOCContainer.Resolve<IGNSContext>();
            _geoDataRepository = IOCManager.IOCContainer.Resolve<IRepository<GeoData>>();
            _geoExtensionDataRepository = IOCManager.IOCContainer.Resolve<IRepository<GeoExtensionData>>(); 
            _geoDataRepository.SetContext(_context);
            _geoExtensionDataRepository.SetContext(_context);
            _localeRepository.SetContext(_context);
            _locationRepository.SetContext(_context);
            _userRepository.SetContext(_context);
            _userSettingsRepository.SetContext(_context);
        }

        /// <summary>
        /// Verifies alerts for all subscribed users
        /// </summary>
        public static void VerifyAlertsForAllUsers()
        {
            UserManger userManager = new UserManger(_userRepository);
            List<User> users = userManager.GetAllUsers();
            foreach (User user in users)
            {
                CheckAlerts(user);
            }
        }

        private static void CheckAlerts(User user)
        {
           List<BaseAlert> alerts = GetAlertsForUser(user.UserName);
           SettingsManager settingsManager = new SettingsManager(_userSettingsRepository);
           UserSettings settings = settingsManager.GetUserSettings(user.UserName); 
            if(settings.UseEmail)
            {
                MailHelper.SendNotificationEmail(user, alerts);
            }
        }

        /// <summary>
        /// Returns list of alerts for user location 
        /// </summary>
        /// <param name="username">User name</param>
        /// <returns>List of base alerts</returns>
        public static List<BaseAlert> GetAlertsForUser(string username)
        {
            StatisticManager statisticManager = new StatisticManager(_geoDataRepository, _geoExtensionDataRepository);
            double lat = 49.9966392, lon = 36.2572576;
            var wander_temp = new WandergroundAlerts();
            IAlert wAlertTemp = wander_temp.CreateTemperatureAlert(lat, lon, 30, 3);
            //GNSCore.Helpers.ContextHelper.Instance.Context.;
            //
            List<BaseAlert> listBaseAlert = wAlertTemp.CheckAlert();
            statisticManager.SaveStatistic(listBaseAlert);

             
            /*_context = IOCManager.IOCContainer.Resolve<IGNSContext>();
            _settingsRepository.SetContext(_context);*/
            
            return listBaseAlert;
            //LocationManger locationManager = new LocationManger(_locationRepository);
            //Location location = locationManager.GetLocationForUser(username);
            //WandergroundAlerts wundergroundAlerts = new WandergroundAlerts();
            //IAlert wanderAlertGlobal = wundergroundAlerts.CreateGlobalAlert(location.Latitude, location.Longitude, location.Radius);
            ////to what this? return wanderAlertGlobal.CheckAlert(location.Latitude, location.Longitude, location.Radius);
            //return wanderAlertGlobal.CheckAlert();
        }
    }
}
